﻿using Engine;
using Game.NetWork.Packages;
using Game.NetWork;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System;
using Game.IContentReader;
using System.Text;

namespace Game.Server.Plugins
{
    public class PlayerDemoZHPlugin : ServerPlugin, IPlayerEnterGameEventHandle
    {
        public override int Version => 10000;

        public override string Name => "新人游玩体验";

        public byte FirstLevel => 0;

        private float PlayDuration = 60f; //游玩体验60秒

        public override void Initialize()
        {
            PlayerEnterGameEventManager.AddObject(this);
        }

        public void PlayerEnter(PlayerData playerData)
        {
            if (playerData == null || playerData.Client == null) return;
            string api = $"https://m.schub.top/com/api/user/info?id={playerData.Client.CommunityAccountId}";
            WebManager.Get(api, null, null, new CancellableProgress(), delegate (byte[] result)
            {
                //获取成功执行
                try
                {
                    var json = (SimpleJson.JsonObject)WebManager.JsonFromBytes(result);
                    if (json["code"].ToString() == "200")
                    {
                        var data = (SimpleJson.JsonObject)json["data"];
                        int regTime = int.Parse(data["regtime"].ToString());
                        long nowTick = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
                        int oneWeek = 3600 * 24 * 7;
                        if(nowTick - regTime < oneWeek) //注册时间短于一周
                        {
                            Time.QueueTimeDelayedExecution(Time.RealTime + 5f, delegate
                            {
                                if (playerData == null || playerData.Client == null) return;
                                DisplaySmallMessageNet(playerData.ComponentPlayer, "系统检测到你为新号玩家，只能游玩1分钟"); //5秒后弹出提示
                            });
                            Time.QueueTimeDelayedExecution(Time.RealTime + PlayDuration, delegate 
                            {
                                if (playerData == null || playerData.Client == null) return;
                                CommonLib.Net.RemoveClient(playerData.Client, "系统检测到你为新号玩家，只能游玩1分钟，游玩体验时间已到");
                            });
                        }
                    }
                    else
                    {
                        Log.Error("PlayerDemoPlugin:" + json["msg"].ToString());
                    }
                }
                catch (Exception e)
                {
                    Log.Error("PlayerDemoPlugin:" + e.Message);
                }

            }, delegate (Exception e2) {
                //获取失败执行
                Log.Error("PlayerDemoPlugin:" + e2.Message);
            });
        }

        public void PlayerLeave(PlayerData playerData)
        {
        }

        /// <summary>
        /// 给指定玩家发送提示
        /// </summary>
        /// <param name="componentPlayer"></param>
        /// <param name="text"></param>
        public void DisplaySmallMessageNet(ComponentPlayer componentPlayer, string text)
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }

        public override void Load()
        {
        }

        public override void Save()
        {
        }
    }
}